package net.technics;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import net.hibernate.HibernateUtil;
import net.models.Application;
import net.models.Groups;
import net.models.Praticiens;
import net.models.TypesPraticiens;
import net.models.Users;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.property.Properties;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.events.VerifyEvent;

public class UtilisateurAjout {
	private DataBindingContext m_bindingContext;

	protected Shell shlBackOfficeManagement;
	private Text textUserNom;
	private Text textUserPrenom;
	private Text textUserAdresse;
	private Text textUserCp;
	private Text textUserVille;
	private Text textUserUsername;
	private Text textUserPassword;
	private Text txtAjouterUnUtilisateur;
	private List<Groups> groups = new ArrayList<Groups>();
	private ComboViewer comboViewer;
	private Combo cmbUserGroupe;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Display display = Display.getDefault();
		Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
			public void run() {
				try {
					UtilisateurAjout window = new UtilisateurAjout();
					window.open();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Open the window.
	 */
	public void open() {
		groups = Application.getGroups();
		Display display = Display.getDefault();
		createContents();
		shlBackOfficeManagement.open();
		shlBackOfficeManagement.layout();
		while (!shlBackOfficeManagement.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shlBackOfficeManagement = new Shell();
		shlBackOfficeManagement.setText("Back Office Management");
		shlBackOfficeManagement.setSize(302, 467);

		Composite composite = new Composite(shlBackOfficeManagement, SWT.NONE);
		composite.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_DARK_BLUE));
		composite.setBounds(0, 0, 284, 48);

		txtAjouterUnUtilisateur = new Text(composite, SWT.READ_ONLY);
		txtAjouterUnUtilisateur.setFont(SWTResourceManager.getFont("Segoe UI",
				11, SWT.BOLD));
		txtAjouterUnUtilisateur.setForeground(SWTResourceManager
				.getColor(SWT.COLOR_WHITE));
		txtAjouterUnUtilisateur.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_DARK_BLUE));
		txtAjouterUnUtilisateur.setText("Ajouter un utilisateur");
		txtAjouterUnUtilisateur.setBounds(10, 10, 192, 21);

		Composite composite_1 = new Composite(shlBackOfficeManagement, SWT.NONE);
		composite_1.setBounds(10, 54, 274, 312);

		CLabel lblNom = new CLabel(composite_1, SWT.NONE);
		lblNom.setBounds(10, 10, 61, 21);
		lblNom.setText("Nom");

		CLabel lblPrnom = new CLabel(composite_1, SWT.NONE);
		lblPrnom.setText("Prénom");
		lblPrnom.setBounds(10, 43, 61, 21);

		CLabel lblAdresse = new CLabel(composite_1, SWT.NONE);
		lblAdresse.setText("Adresse");
		lblAdresse.setBounds(10, 76, 61, 21);

		CLabel lblCodePostal = new CLabel(composite_1, SWT.NONE);
		lblCodePostal.setText("Code postal");
		lblCodePostal.setBounds(10, 109, 86, 21);

		textUserNom = new Text(composite_1, SWT.BORDER);
		textUserNom.setBounds(134, 10, 130, 26);

		textUserPrenom = new Text(composite_1, SWT.BORDER);
		textUserPrenom.setBounds(134, 43, 130, 26);

		textUserAdresse = new Text(composite_1, SWT.BORDER);
		textUserAdresse.setBounds(134, 76, 130, 26);

		textUserCp = new Text(composite_1, SWT.BORDER);
		textUserCp.addVerifyListener(new VerifyListener() {
			public void verifyText(VerifyEvent arg0) {
				if(!Character.isDigit(arg0.character))
					arg0.doit=false;
			}
			
		});
		textUserCp.setBounds(134, 109, 130, 26);

		CLabel lblVille = new CLabel(composite_1, SWT.NONE);
		lblVille.setBounds(10, 142, 61, 21);
		lblVille.setText("Ville");

		textUserVille = new Text(composite_1, SWT.BORDER);
		textUserVille.setBounds(134, 142, 130, 26);

		CLabel lblDateDembauche = new CLabel(composite_1, SWT.NONE);
		lblDateDembauche.setBounds(10, 175, 124, 21);
		lblDateDembauche.setText("Date d'embauche");

		final DateTime dateTime = new DateTime(composite_1, SWT.BORDER);
		dateTime.setBounds(134, 175, 130, 26);

		CLabel lblUsername = new CLabel(composite_1, SWT.NONE);
		lblUsername.setBounds(10, 208, 72, 21);
		lblUsername.setText("Username");

		textUserUsername = new Text(composite_1, SWT.BORDER);
		textUserUsername.setBounds(134, 208, 130, 26);

		textUserPassword = new Text(composite_1, SWT.BORDER | SWT.PASSWORD);
		textUserPassword.setBounds(134, 241, 130, 26);

		CLabel lblPassword = new CLabel(composite_1, SWT.NONE);
		lblPassword.setBounds(10, 241, 72, 21);
		lblPassword.setText("Password");

		comboViewer = new ComboViewer(composite_1, SWT.NONE);
		cmbUserGroupe = comboViewer.getCombo();
		cmbUserGroupe.setBounds(134, 274, 130, 28);

		CLabel lblGroupe = new CLabel(composite_1, SWT.NONE);
		lblGroupe.setBounds(10, 274, 61, 21);
		lblGroupe.setText("Groupe");

		Composite composite_2 = new Composite(shlBackOfficeManagement, SWT.NONE);
		composite_2.setBounds(20, 372, 264, 52);

		Button button = new Button(composite_2, SWT.NONE);
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				shlBackOfficeManagement.close();
			}
		});
		button.setText("Annuler");
		button.setBounds(164, 10, 90, 30);

		Button btnAjouter = new Button(composite_2, SWT.NONE);
		btnAjouter.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				Session session = HibernateUtil.getSession();
				Transaction trans = session.beginTransaction();
				IStructuredSelection sel = (IStructuredSelection) (comboViewer.getSelection());
				Groups group = (Groups) (sel.iterator().next());
				Calendar instance = Calendar.getInstance();
				instance.set(Calendar.DAY_OF_MONTH, dateTime.getDay());
				instance.set(Calendar.MONTH, dateTime.getMonth());
				instance.set(Calendar.YEAR, dateTime.getYear());
			
				Users user = new Users(textUserNom.getText(), textUserPrenom
						.getText(), textUserAdresse.getText(), textUserCp
						.getText(), textUserVille.getText(),instance.getTime(), textUserUsername
						.getText(), textUserPassword.getText(), group);
				session.persist(user);
				trans.commit();
				session.close();
				shlBackOfficeManagement.close();
			}
		});

		btnAjouter.setText("Valider");
		btnAjouter.setBounds(68, 10, 90, 30);
		m_bindingContext = initDataBindings();

	}

	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
		IObservableMap observeMap = BeansObservables.observeMap(
				listContentProvider.getKnownElements(), Groups.class, "name");
		comboViewer
				.setLabelProvider(new ObservableMapLabelProvider(observeMap));
		comboViewer.setContentProvider(listContentProvider);
		//
		IObservableList selfList = Properties.selfList(Groups.class).observe(
				groups);
		comboViewer.setInput(selfList);
		//
		return bindingContext;
	}
}
